package com.cwt_taxi_xa.protocol.card;



import com.cwt_taxi_xa.protocol.CardUtil;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;
/**
 * 射频打开关闭管理
 * @author Administrator
 *
 */
public abstract class CardRFState implements CardState {

	private String RFTAG = "CardRFState";

	protected boolean openRadioFrequency = false;

	private boolean startRead = false;

	private Thread mThread;

	public boolean isOpenRadioFrequency() {
		return openRadioFrequency;
	}


	public void setOpenRadioFrequency(boolean openRadioFrequency) {
		this.openRadioFrequency = openRadioFrequency;
		AppLog.d(RFTAG,"openRadioFrequency:" + openRadioFrequency  + " " + AppLog.generateTag());
	}



	public void setStartRead(boolean startRead) {
		this.startRead = startRead;
	}



	public boolean isStartRead() {
		return startRead;
	}




	public abstract long delay();

	@Override
	public void startAndRead() {
		// TODO Auto-generated method stub
		setStartRead(true);
		readCard();
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		AppLog.d(RFTAG, "start() rf open:" + isOpenRadioFrequency());
		setStartRead(false);
		if(!isOpenRadioFrequency()){
			//如果没有打开射频,启动打开射频定时器
			startThread();
		}
	}

	private void readCard(){
		if(isOpenRadioFrequency() && isStartRead()){
			CardUtil.sendTsm905Data(0x0020, null);
			AppLog.d(RFTAG,"startAndRead readCard()");
		}
	}

	@Override
	public void receivePackage(String mid, String result, byte[] buffer) {
		// TODO Auto-generated method stub
		if(result.equals("9000")){
			if(mid.equals("0015")){
				setOpenRadioFrequency(true);
				readCard();
				stopThread();
				AppLog.d(RFTAG,"receivePackage  0015 射频已打开");
			}else if(mid.equals("0016")){
				setOpenRadioFrequency(false);
				stopThread();
				AppLog.d(RFTAG,"receivePackage  0016 射频已关闭");
			}
		}
	}


	//public abstract void packageData(String mid, String result, byte[] buffer);

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		stopThread();
		AppLog.d(RFTAG, "stop() rf open:" + isOpenRadioFrequency());
		if(isOpenRadioFrequency()){
			//关闭射频
			CardUtil.sendTsm905Data(0x0016, Tools.parseHexStr2Byte("00"));
		}
	}


	protected synchronized void startThread() {
		if (mThread == null) {
			mThread = new Thread(new Runnable() {
				int currentIndex = 0;

				@Override
				public void run() {
					// TODO Auto-generated method stub
					AppLog.d(RFTAG, "1 射频 Thread.isInterrupted()" + Thread.currentThread().isInterrupted());
					try {
						while (!Thread.currentThread().isInterrupted() && currentIndex < 10) {

							AppLog.d(RFTAG, "射频 Thread.isInterrupted()" + Thread.currentThread().isInterrupted());
							AppLog.d(RFTAG, "射频 Thread.isInterrupted()");
							try {
								// 打开射频
								CardUtil.sendTsm905Data(0x0015, Tools.parseHexStr2Byte("00"));
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							currentIndex ++;
							Thread.sleep(800);

						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						// Thread.currentThread().interrupt();
					}
				}
			});
			mThread.start();
		}
	}

	protected synchronized void stopThread(){
		if(mThread != null){
			mThread.interrupt();
			mThread = null;
		}

	}
}
